package top.igotcha.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @time 2021/1/10
 * @auth Gotcha
 * @describe
 */
public class HdfsGetFileSystem {

    //获取文件系统的第一种方式
    @Test
    public void getFileSystem() throws URISyntaxException, IOException {
        //1.获取Configuration对象
        Configuration configuration = new Configuration();
        //2.设置Configuration对象，指定要操作的文件系统
        configuration.set("fs.defaultFS","hdfs://192.168.127.110:8020");
        //3.获取指定文件系统，获取了fileSystem就相当于获取了主节点中所有元数据信息
        FileSystem fileSystem = FileSystem.get(configuration);
        System.out.println(fileSystem.toString());
    }

    //获取文件系统的第二种方式
    @Test
    public void getFileSystem2() throws URISyntaxException, IOException {
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.127.110:8020"), new Configuration());
        System.out.println(fileSystem.toString());
    }

    //获取文件系统的第三种方式
    @Test
    public void getFileSystem3() throws URISyntaxException, IOException {
        FileSystem fileSystem = FileSystem.newInstance(new URI("hdfs://192.168.127.110:8020"), new Configuration());
        System.out.println(fileSystem.toString());
    }

    //获取文件系统的第四种方式
    @Test
    public void getFileSystem4() throws  Exception{
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS","hdfs://192.168.127.110:8020");
        FileSystem fileSystem = FileSystem.newInstance(configuration);
        System.out.println(fileSystem.toString());
    }
}
